//
//  PEMAddMotionRecordController.swift
//  RelationshipIntegration
//
//  Created by iOS-Czz on 2023/11/16.
//

import UIKit
import BRPickerView
import ProgressHUD

class PEMAddMotionRecordController: PEMBaseViewController {
    @IBOutlet var runningBtn: UIButton!
    @IBOutlet var ridingBtn: UIButton!
    @IBOutlet var otherBtn: UIButton!
    @IBOutlet var distanceTF: UITextField!
    @IBOutlet var durationTF: UITextField!
    @IBOutlet var stepTF: UITextField!
    
    @IBOutlet var energyTF: UITextField!
    @IBOutlet var nameTF: UITextField!
    @IBOutlet var dateTF: UITextField!
    var type = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        clickRunningAction("")
        // Do any additional setup after loading the view.
    }
    @IBAction func gotoBack(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    @IBAction func clickSaveAction(_ sender: Any) {
        self.view.endEditing(true)
        if self.dateTF.text!.isEmpty {
            ProgressHUD.showError("Please select date of sport")
            return
        }
        if self.nameTF.text!.isEmpty {
            ProgressHUD.showError("Please enter sports content")
            return
        }
        if self.distanceTF.text!.isEmpty {
            ProgressHUD.showError("Please enter movement distance")
            return
        }
        if self.durationTF.text!.isEmpty {
            ProgressHUD.showError("Please enter exercise time")
            return
        }
        if self.stepTF.text!.isEmpty {
            ProgressHUD.showError("Please enter number of exercise steps")
            return
        }
        if self.energyTF.text!.isEmpty {
            ProgressHUD.showError("Please enter exercise cost")
            return
        }
        /**
         var name = ""
         var type = 0
         var duration = 0.0
         var distance = 0.0
         var step = 0
         var engery = 0
         var date = ""
         var isDefault = true
         */
        let model = PEMDailyExerciseModel()
        model.date = self.dateTF.text!
        model.name = self.nameTF.text!
        model.type = self.type
        model.duration = Double(self.durationTF.text!)!
        model.distance = Double(self.distanceTF.text!)!
        model.step = Int(self.stepTF.text!)!
        model.engery = Int(self.energyTF.text!)!
        model.isDefault = false
        model.saveToDB()
        let helper = PEMDailyExerciseModel.getUsingLKDBHelper()
        helper.delete(with: PEMDailyExerciseModel.self, where: "isDefault = true")
        ProgressHUD.showSuccess("More record addition succeed")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popViewController(animated: true)
        }
    }
    @IBAction func clickRunningAction(_ sender: Any) {
        self.view.endEditing(true)
        self.type = 0
        self.runningBtn.isSelected = true
        self.ridingBtn.isSelected = false
        self.otherBtn.isSelected = false
    }
    @IBAction func clickRidingAction(_ sender: Any) {
        self.view.endEditing(true)
        self.type = 1
        self.runningBtn.isSelected = false
        self.ridingBtn.isSelected = true
        self.otherBtn.isSelected = false
    }
    @IBAction func clickOtherAction(_ sender: Any) {
        self.view.endEditing(true)
        self.type = 2
        self.runningBtn.isSelected = false
        self.ridingBtn.isSelected = false
        self.otherBtn.isSelected = true
    }
    @IBAction func clickDateAction(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRDatePickerView(pickerMode: .YMD)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.resultBlock = { selectDate,selectValue in
            self.dateTF.text = selectDate?.toString(format: "yyyy.MM.dd")
        }
        pickerView.show()
    }
}
